package demo.kk.demo2;

import java.util.*;

public class Main {
    // Trie 结构定义
    static class TrieNode {
        boolean isWord;
        TrieNode[] children = new TrieNode[26];

        TrieNode() {
            Arrays.fill(children, null);
        }
    }

    static TrieNode root = new TrieNode();

    // 插入单词
    static void insertWord(String word) {
        TrieNode currentNode = root;
        for (char c : word.toCharArray()) {
            int index = c - 'a';
            if (currentNode.children[index] == null) {
                currentNode.children[index] = new TrieNode();
            }
            currentNode = currentNode.children[index];
        }
        currentNode.isWord = true;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取输入，并转换为小写
        String sentence = scanner.nextLine().toLowerCase();
        String dictionary = scanner.nextLine();
        scanner.close();

        // 插入字典中的单词
        for (String word : dictionary.split(",")) {
            insertWord(word);
        }

        List<String> result = new ArrayList<>();
        int startIndex = 0, sentenceSize = sentence.length();

        while (startIndex < sentenceSize) {
            // 如果当前字符不是字母，标点符号直接跳过
            if (!Character.isLetter(sentence.charAt(startIndex))) {
                startIndex++;
                continue;
            }

            int lastValidIndex = -1;
            TrieNode currentNode = root;

            // 从 startIndex 开始，寻找最长匹配的单词
            for (int i = startIndex; i < sentenceSize; i++) {
                char c = sentence.charAt(i);
                if (!Character.isLetter(c) || currentNode.children[c - 'a'] == null) {
                    break;
                }
                currentNode = currentNode.children[c - 'a'];
                //记录当前最大分割位置
                if (currentNode.isWord) {
                    lastValidIndex = i;
                }
            }
            // 切割
            if (lastValidIndex != -1) {
                result.add(sentence.substring(startIndex, lastValidIndex + 1));
                startIndex = lastValidIndex + 1;
            } else {
                result.add(String.valueOf(sentence.charAt(startIndex++)));
            }
        }

        // 结果输出
        System.out.println(String.join(",", result));
    }
}

